import QtQuick 1.1
//import com.nokia.symbian 1.1
import com.nokia.meego 1.0

Page {
    property variant coin

    function isVertical() {
        return (screen.currentOrientation === Screen.Portrait || screen.currentOrientation === Screen.PortraitInverted);
    }

    onStatusChanged: {
        if (status === PageStatus.Active || status === PageStatus.Activating) {
            details.contentX = 0;
            details.contentY = 0;
        }
        if (status === PageStatus.Activating) {
            gridModel.fill();
        }
    }

    Rectangle {
        anchors.fill: parent
        color: visual.colorBackgrond
    }

    Component {
        id: gridDelegate

        Text {
            text: getText(index)
            font.pixelSize: visual.fontDefault
            font.bold: (index % 2) ? true : false
            elide: Text.ElideRight
            function getText(index) {
                if (index >= 0 && index < gridModel.count)
                    return gridModel.get(index).val;
                else
                    return '';
            }
        }
    }

    ListModel {
        id: gridModel
        function fill() {
            var val;
            gridModel.clear();
            if (coin.country) {
                gridModel.append({'val': qsTr("Country: ")});
                gridModel.append({'val': coin.country});
            }
            if (coin.value) {
                gridModel.append({'val': qsTr("Denomination: ")});
                gridModel.append({'val': coin.value + ' ' + coin.unit});
            }
            if (coin.subjectshort) {
                gridModel.append({'val': qsTr("Subject: ")});
                gridModel.append({'val': coin.subjectshort});
            }
            if (coin.series) {
                gridModel.append({'val': qsTr("Series: ")});
                gridModel.append({'val': coin.series});
            }
            if (coin.issuedate) {
                gridModel.append({'val': qsTr("Date of issue: ")});
                gridModel.append({'val': Qt.formatDate(new Date(coin.issuedate.replace(/(\d+)-(\d+)-(\d+)/, '$2/$3/$1')), qsTr("dd.MM.yyyy"))});
            }
            else if (coin.year) {
                gridModel.append({'val': qsTr("Year: ")});
                gridModel.append({'val': coin.year});
            }
            if (coin.mintage) {
                gridModel.append({'val': qsTr("Mintage: ")});
                gridModel.append({'val': coin.mintage.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ")});
            }
            if (coin.mint) {
                gridModel.append({'val': qsTr("Mint: ")});
                gridModel.append({'val': coin.mint + ' (' + coin.mintmark + ')'});
            }
            if (coin.price4 || coin.price3 || coin.price2 || coin.price1) {
                gridModel.append({'val': qsTr("Price: ")});
                if (coin.price4) {
                    val = coin.price4 + qsTr(' (uncirculated)');
                    if (coin.price3)
                        val = val + ' / ' + coin.price3 + qsTr(' (extremely fine)');
                    else if (coin.price2)
                        val = val + ' / ' + coin.price2 + qsTr(' (very fine)');
                    else if (coin.price1)
                        val = val + ' / ' + coin.price1 + qsTr(' (fine)');
                    gridModel.append({'val': val});
                }
                else if (coin.price3) {
                    val = coin.price3 + qsTr(' (extremely fine)');
                    if (coin.price2)
                        val = val + ' / ' + coin.price2 + qsTr(' (very fine)');
                    else if (coin.price1)
                        val = val + ' / ' + coin.price1 + qsTr(' (fine)');
                    gridModel.append({'val': val});
                }
                else if (coin.price2) {
                    val = coin.price2 + qsTr(' (very fine)');
                    if (coin.price1)
                        val = val + ' / ' + coin.price1 + qsTr(' (fine)');
                    gridModel.append({'val': val});
                }
                else {
                    gridModel.append({'val': coin.price1 + qsTr(' (fine)')});
                }
            }
            if (coin.material) {
                gridModel.append({'val': qsTr("Metal: ")});
                val = coin.material;
                if (coin.fineness)
                    val = val + ' (' + coin.fineness + ')';
                gridModel.append({'val': val});
            }
        }
    }

    Flickable {
        id: details
        width: parent.width
        contentWidth: width
        anchors.top: viewHeader.bottom
        anchors.bottom: parent.bottom
        contentHeight: column.height

        Column {
            id: column
            width: parent.width
            spacing: visual.defaultMargin

            Grid {
                columns: isVertical() ? 1 : 2
                rows: (2 - columns) + 1
                spacing: visual.defaultMargin

                Grid {
                    columns: parent.rows
                    rows: (2 - columns) + 1

                    Image {
                        width: isVertical() ? (details.width / 2) : (details.height / 2)
                        height: width
                        source: detailsPage.status === PageStatus.Active ? "data:image/png;base64," + coin.obverseimg : ""
                        smooth: true
                        fillMode: Image.PreserveAspectFit
                    }
                    Image {
                        width: isVertical() ? (details.width / 2) : (details.height / 2)
                        height: width
                        source: detailsPage.status === PageStatus.Active ? "data:image/png;base64," + coin.reverseimg : ""
                        smooth: true
                        fillMode: Image.PreserveAspectFit
                    }

                    MouseArea {
                        width: isVertical() ? details.width : (details.height / 2)
                        height: isVertical() ? (details.width / 2) : details.height
                        onClicked: {
                            imagePage.coin = coin;
                            pageStack.push(imagePage);
                        }
                    }
                }

                Grid {
                    columns: 2

                    Repeater {
                        model: gridModel
                        delegate: gridDelegate
                    }
                }
            }

            Text {
                width: parent.width
                text: coin ? coin.subject : ''
                font.pixelSize: visual.fontDefault
                wrapMode: Text.Wrap
            }
        }
    }

    ScrollDecorator {
        flickableItem: details
    }

    ViewHeader {
        id: viewHeader
        clickable: false
        titleText: coin ? coin.title : ''
    }

    tools: ToolBarLayout {
        ToolIcon {
            platformIconId: "toolbar-back"
            onClicked: pageStack.pop()
        }
    }
}
